home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / MATHS / RLAB / RLAB125.ZIP / !RLaB / examples / mgs < prev    next >
Text File  |  1994-09-20  |  594b  |  28 lines

  1. //
  2. // Modified Gram-Schmidt
  3. // Given A (MxN), with rank(A) = N. The following algorithm computes
  4. // the factorization A = Q*R (skinny QR) where Q (MxN) has orthonormal
  5. // columns and R (NxN) is upper triangular
  6. //
  7. // From MATRIX Computations, G.H. Golub, C.F. Van Loan (page 219)
  8. // 
  9.  
  10. mgs = function(A)
  11. {
  12.   local (A)
  13.  
  14.   m = A.nr;
  15.   n = A.nc;
  16.   for(k in 1:n)
  17.     {
  18.       r[k;k] = norm( A[1:m;k], "2");
  19.       q[1:m;k] = A[1:m;k]/r[k;k];
  20.       for(j in k+1:n)
  21.     {
  22.       r[k;j] = q[1:m;k]' * A[1:m;j];
  23.           A[1:m;j] = A[1:m;j] - q[1:m;k] * r[k;j];
  24.         }
  25.      }
  26.   return << q = q; r = r >>;
  27. };
  28.